<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\LihuaOrderInvo;
use app\common\model\LihuaOrderInvoreq;
use app\common\model\general\LihuaCity;
use app\common\model\LihuaOrder;


/**
 * 示例接口
 */
class Invoice extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['getorderinvoice', 'getorderinvoice_req', 'update_invoice_reqstatus', 'demo'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /*demo*/
    public function demo()
    {
        $this->success('demo');    
    }

    /**
     * 获取订单的发票
     */
    public function getorderinvoice()
    {
        $sn = $this->request->request('sn', ''); //订单SN
        $url = $this->request->request('url', ''); //发票地址
        $url_app = $this->request->request('url_app', ''); //app端发票地址
        $city = $this->request->request('city', ''); //城市名称

        if (!$city || !$url || !$url_app || !$sn) $this->error('参数错误');

        //根据城市名称获取city_id
        $city_info  = LihuaCity::getCityInfo($city);
        $city_id = $city_info['city_id'];
        
        //查询是否已经绑定过发票
        $has = LihuaOrderInvo::where(['order_sn'=>$sn])->find();
        $data = [
            'order_sn' => $sn,
            'city_id' => $city_id,
            'url' => $url,
            'url_app' => $url_app,
            'status' => 1,
            'create_time' => date('Y-m-d H:i:s'),
        ];
        if(!$has){
            $res = LihuaOrderInvo::insert($data);
            $this->success('返回成功', $this->request->param());
        }else{
            LihuaOrderInvo::update($data, ['id' => $has['id']]);
            $this->success('发票信息更新成功', $this->request->param());       
        }
    }

    //订单详情获取发票信息
    public function orderinvoice(){ 
        $sn = $this->request->request('sn', ''); //订单SN 
        if (!$sn) $this->error('参数错误'); 
        //查询是否已经绑定过发票 
        $has = LihuaOrderInvo::where(['order_sn'=>$sn])->find(); 
        if(!$has){
            $this->error('暂无发票信息');
        }else{
            $this->success('OK', $has);
        }
    }

    //获取发票申请记录
    public function getorderinvoice_req(){
        $list = LihuaOrderInvoreq::where(['status'=>1])->limit(50)->select();
        if(!$list){
            $this->error('暂无发票信息');
        }else{
            $this->success('OK', $list);
        }
    }

    //新增发票申请，此接口客户端调用，需要登录的登录的情况下进行
    public function add_orderinvoice_req(){
        $sn = $this->request->request('sn', ''); //订单SN 
        //获取订单信息
        $order = LihuaOrder::where(['sn'=> $sn])->field('city_id,sn')->find();
        $data = [
            'order_sn' => $sn,
            'city_id' => $order['city_id'],
            'status' => 1,
            'create_time' => date('Y-m-d H:i:s'),
        ];
        $res = LihuaOrderInvoreq::insert($data);
        if(!$res){
            $this->error('发票申请失败');
        }else{
            $this->success('OK', $data);       
        }
    }


    //更新发票申请记录状态
    public function update_invoice_reqstatus(){
        $sn = $this->request->request('sn', ''); //订单SN 
        $status = (int)$this->request->request('status', ''); //状态值
        if ( !$sn || !$status ) $this->error('参数错误'); 
        $res = LihuaOrderInvoreq::update(['status'=> $status], ['order_sn' => $sn]);
        if($status==0){
            //查询是否已经开过发票 如果已经生成的情况下需要将原来的发票作废
            $i = LihuaOrderInvo::where(['order_sn' => $sn])->find();
            if($i) $res2 = LihuaOrderInvo::update(['status'=> $status], ['order_sn' => $sn]);
        }
        if(!$res){
            $this->error('修改状态失败');
        }else{
            $this->success('OK', $res);       
        }
    }
}
